package com.example.timeassistant.network

import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Query

data class LoginRequest(
    val username_or_email: String,
    val password: String
)

// /loginApi 登录请求与响应
data class LoginApiRequest(
    val account: String,
    val password: String,
    val rememberMe: Boolean = true,
    val tenantCode: String? = null
)

data class LoginApiData(
    val userId: String?,
    val token: String?,
    val ssoLogin: String?,
    val ssoLoginCode: String?
)

data class TokenResponse(
    val access_token: String
)

// 通用响应封装（适配后端 ResponseData）
data class ResponseEnvelope<T>(
    val success: Boolean?,
    val code: String?,
    val message: String?,
    val data: T?
)

// 注册与 App 用户登录的数据模型
data class RegisterRequest(
    val account: String,
    val password: String,
    val nickname: String? = null,
    val registrationId: String? = null,
    val sysUserId: Long? = null
)
data class AppUserDto(
    val userId: Long?,
    val account: String?,
    val nickname: String?,
    val statusFlag: String?
)

interface ApiService {
    @POST("auth/login")
    fun login(@Body payload: LoginRequest): Call<TokenResponse>

    // 后台管理统一登录接口
    @POST("loginApi")
    fun loginApi(@Body payload: LoginApiRequest): Call<ResponseEnvelope<LoginApiData>>

    // 注册 App 用户
    @POST("appUser/register")
    fun register(@Body payload: RegisterRequest): Call<ResponseEnvelope<AppUserDto>>

    // App 用户登录（不返回 token，仅用于演示）
    @POST("appUser/login")
    fun appUserLogin(@Body payload: RegisterRequest): Call<ResponseEnvelope<AppUserDto>>

    // ===== 任务相关数据模型与接口 =====
    data class TaskItem(
        val taskId: Long?,
        val title: String?,
        val description: String?,
        val dueDate: String?,
        val priority: String?,
        val status: String?,
        val pinned: String?,
        val userId: Long?
    )

    data class PageResult<T>(
        val rows: List<T>?,
        val totalRows: Int?,
        val pageNo: Int?,
        val pageSize: Int?
    )

    data class TaskSubmit(
        val taskId: Long? = null,
        val title: String? = null,
        val description: String? = null,
        val dueDate: String? = null,
        val priority: String? = null,
        val completed: Boolean? = null,
        val pinned: String? = null
    )

    @GET("tasks/page")
    fun getTasksPage(
        @Query("searchText") searchText: String? = null,
        @Query("pageNo") pageNo: Int = 1,
        @Query("pageSize") pageSize: Int = 10,
        @Query("sortField") sortField: String? = null,
        @Query("sortOrder") sortOrder: String? = null,
        @Query("completed") completed: Boolean? = null,
        @Query("priority") priority: String? = null
    ): Call<ResponseEnvelope<PageResult<TaskItem>>>

    @POST("tasks/add")
    fun addTask(@Body payload: TaskSubmit): Call<ResponseEnvelope<Any?>>

    @POST("tasks/edit")
    fun editTask(@Body payload: TaskSubmit): Call<ResponseEnvelope<Any?>>

    @POST("tasks/delete")
    fun deleteTask(@Body payload: TaskSubmit): Call<ResponseEnvelope<Any?>>

    @POST("tasks/updateStatus")
    fun updateTaskStatus(@Body payload: TaskSubmit): Call<ResponseEnvelope<Any?>>

    @POST("tasks/updatePinned")
    fun updateTaskPinned(@Body payload: TaskSubmit): Call<ResponseEnvelope<Any?>>

    // ===== 记账相关数据模型与接口 =====
    data class TransactionItem(
        val transactionId: Long?,
        val amount: Double?,
        val type: String?,
        val category: String?,
        val transactionDate: String?,
        val note: String?
    )

    data class TransactionSubmit(
        val transactionId: Long? = null,
        val amount: Double? = null,
        val type: String? = null,
        val category: String? = null,
        val transactionDate: String? = null,
        val note: String? = null,
        val pageNo: Int? = null,
        val pageSize: Int? = null,
        val startDate: String? = null,
        val endDate: String? = null
    )

    data class CategoryStat(
        val category: String?,
        val amount: Double?
    )

    @GET("account/page")
    fun getTransactionsPage(
        @Query("pageNo") pageNo: Int = 1,
        @Query("pageSize") pageSize: Int = 10,
        @Query("startDate") startDate: String? = null,
        @Query("endDate") endDate: String? = null,
        @Query("category") category: String? = null
    ): Call<ResponseEnvelope<PageResult<TransactionItem>>>

    @POST("account/add")
    fun addTransaction(@Body payload: TransactionSubmit): Call<ResponseEnvelope<Any?>>

    @POST("account/edit")
    fun editTransaction(@Body payload: TransactionSubmit): Call<ResponseEnvelope<Any?>>

    @POST("account/delete")
    fun deleteTransaction(@Body payload: TransactionSubmit): Call<ResponseEnvelope<Any?>>

    @GET("account/stats/category")
    fun getCategoryStats(
        @Query("startDate") startDate: String? = null,
        @Query("endDate") endDate: String? = null
    ): Call<ResponseEnvelope<List<CategoryStat>>>

    // ===== 预算相关 =====
    data class BudgetItem(
        val budgetId: Long?,
        val category: String?,
        val amount: Double?,
        val startDate: String?,
        val endDate: String?
    )

    data class BudgetSubmit(
        val budgetId: Long? = null,
        val category: String? = null,
        val amount: Double? = null,
        val startDate: String? = null,
        val endDate: String? = null,
        val pageNo: Int? = null,
        val pageSize: Int? = null
    )

    @GET("budget/page")
    fun getBudgetPage(
        @Query("pageNo") pageNo: Int = 1,
        @Query("pageSize") pageSize: Int = 10
    ): Call<ResponseEnvelope<PageResult<BudgetItem>>>

    @POST("budget/add")
    fun addBudget(@Body payload: BudgetSubmit): Call<ResponseEnvelope<Any?>>

    @POST("budget/edit")
    fun editBudget(@Body payload: BudgetSubmit): Call<ResponseEnvelope<Any?>>

    @POST("budget/delete")
    fun deleteBudget(@Body payload: BudgetSubmit): Call<ResponseEnvelope<Any?>>

    // ===== 记事本相关 =====
    data class NoteItem(
        val noteId: Long?,
        val title: String?,
        val content: String?,
        val category: String?
    )

    data class NoteSubmit(
        val noteId: Long? = null,
        val title: String? = null,
        val content: String? = null,
        val category: String? = null,
        val pageNo: Int? = null,
        val pageSize: Int? = null
    )

    @GET("note/page")
    fun getNotesPage(
        @Query("pageNo") pageNo: Int = 1,
        @Query("pageSize") pageSize: Int = 10
    ): Call<ResponseEnvelope<PageResult<NoteItem>>>

    @POST("note/add")
    fun addNote(@Body payload: NoteSubmit): Call<ResponseEnvelope<Any?>>

    @POST("note/edit")
    fun editNote(@Body payload: NoteSubmit): Call<ResponseEnvelope<Any?>>

    @POST("note/delete")
    fun deleteNote(@Body payload: NoteSubmit): Call<ResponseEnvelope<Any?>>

    // ===== 通知中心 =====
    data class NotificationItem(
        val notificationId: Long?,
        val title: String?,
        val content: String?,
        val aiContent: String?,
        val sourceUrl: String?,
        val type: String?,
        val createTime: String?,
        val details: String?
    )

    @GET("notification/page")
    fun getNotificationsPage(
        @Query("pageNo") pageNo: Int = 1,
        @Query("pageSize") pageSize: Int = 10
    ): Call<ResponseEnvelope<PageResult<NotificationItem>>>

    @GET("notification/detail")
    fun getNotificationDetail(
        @Query("notificationId") notificationId: Long
    ): Call<ResponseEnvelope<NotificationItem>>
}
